/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: GanttTask.java 98 2011-08-15 00:45:11Z jchimene@gmail.com $
 */
package com.systasis.sked.client.model;

import java.util.Date;

import com.arcetis.gwt.jsgantt.client.Priority;
import com.arcetis.gwt.jsgantt.client.TaskItem;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.systasis.sked.client.presenter.Builder;

public class GanttTask extends JavaScriptObject {
    public static class GanttTaskBuilder implements Builder<TaskItem> {

	final private DateTimeFormat dtFormat;
	private boolean isGroup; //
	private String pColor; //
	private int pComp; //
	private String pDependsOn; //
	private Date pEnd; //
	private String pID; //
	private String pLink; //
	private int pMile;
	private String pName; //
	private int pOpen;
	private int pParent; //
	private String pResponsible; //
	private Date pStart; //

	public GanttTaskBuilder() {
	    pStart = pEnd = null;
	    pID = pName = pLink = "";
	    pParent = 0;
	    pDependsOn = null;
	    pResponsible = "Noah Body";
	    pColor = Priority.BLACK;
	    isGroup = false;
	    pOpen = 1;
	    pComp = pMile = 0;
	    dtFormat = DateTimeFormat.getFormat("MM/dd/yy");
	}

	@Override
	public TaskItem build() {
	    return getGanttTask(this);
	}

	public GanttTaskBuilder color(String color) {
	    pColor = color;
	    return this;
	}

	public GanttTaskBuilder completionPercent(int comp) {
	    pComp = comp;
	    return this;
	}

	public GanttTaskBuilder depends(String depends) {
	    pDependsOn = depends;
	    return this;
	}

	public GanttTaskBuilder endDate(String endDate) {
	    try {
		this.pEnd = parseDate(endDate, "could not parse endDate (DATE_SHORT): ");
	    } catch (final Exception e) {
		e.printStackTrace();
	    }
	    return this;
	}

	public GanttTaskBuilder id(int id) {
	    pID = Integer.toString(id);
	    return this;
	}

	public GanttTaskBuilder isGroup(boolean isGroup) {
	    this.isGroup = isGroup;
	    return this;
	}

	public GanttTaskBuilder link(String url) {
	    pLink = url;
	    return this;
	}

	public GanttTaskBuilder milestone(int milestone) {
	    pMile = milestone;
	    return this;
	}

	public GanttTaskBuilder name(String taskName) {
	    pName = taskName;
	    return this;
	}

	public GanttTaskBuilder open(int open) {
	    pOpen = open;
	    return this;
	}

	public GanttTaskBuilder parent(int parent) {
	    pParent = parent;
	    return this;
	}

	public GanttTaskBuilder resource(String resourceName) {
	    this.pResponsible = resourceName;
	    return this;
	}

	public GanttTaskBuilder responsible(String responsible) {
	    pResponsible = responsible;
	    return this;
	}

	public GanttTaskBuilder startDate(String startDate) {
	    try {
		this.pStart = parseDate(startDate, "could not parse startDate (DATE_SHORT): ");
	    } catch (final Exception e) {
		e.printStackTrace();
	    }
	    return this;
	}

	private Date parseDate(String date, String exceptionMsg) throws IllegalArgumentException {
	    try {
		return dtFormat.parse(date);
	    } catch (final Exception e3) {
		throw new IllegalArgumentException(exceptionMsg + date);
	    }
	}
    }

    private static TaskItem taskItem;

    private static TaskItem getGanttTask(GanttTaskBuilder builder) {
	taskItem = null == builder.pDependsOn //
	? TaskItem.create(builder.pID //
		, builder.pName //
		, builder.pStart //
		, builder.pEnd //
		, builder.pColor //
		, builder.pLink //
		, builder.pMile //
		, builder.pResponsible //
		, builder.pComp //
		, builder.isGroup //
		, Integer.toString(builder.pParent) //
		, builder.pOpen) //
		: //
		TaskItem.create(builder.pID //
			, builder.pName //
			, builder.pStart //
			, builder.pEnd //
			, builder.pColor //
			, builder.pLink //
			, builder.pMile //
			, builder.pResponsible //
			, builder.pComp //
			, builder.isGroup //
			, Integer.toString(builder.pParent) //
			, builder.pOpen //
			, builder.pDependsOn //
		);
	return taskItem;
    }

    protected GanttTask() {
    }
}
